package com.itic.web.pay.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.distribution.persistence.model.Cate;
import com.itic.ht.mem.distribution.service.CateService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.memberdetail.persistence.model.WebMemberDetail;
import com.itic.ht.mem.memberdetail.service.WebMemberDetailService;
import com.itic.ht.mem.mtype.persistence.model.WebmType;
import com.itic.ht.mem.mtype.service.WebmTypeService;
import com.itic.ht.mem.order.persistence.model.MemberOrder;
import com.itic.ht.mem.order.service.MemberOrderService;
import com.itic.ht.mem.orderdetail.persistence.model.MemberOrderDetail;
import com.itic.ht.mem.orderdetail.service.MemberOrderDetailService;
import com.itic.ht.mem.performance.persistence.model.Performance;
import com.itic.ht.mem.performance.persistence.model.Performancedetail;
import com.itic.ht.mem.performance.service.PerformanceService;
import com.itic.ht.mem.performance.service.PerformancedetailService;
import com.itic.web.mem.order.service.AppOrderService;
import com.itic.web.mem.order.vo.MemberOrderVo;
import com.itic.web.pay.common.PayCommon;
import com.itic.web.pay.config.AlipayConfig;
import com.itic.web.pay.config.AlipayTradeAppPayRequestSon;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;


@Controller
@RequestMapping(value = "/app/pay/zhifubao")
public class ZhifubaoPay {
	@Resource
	private AppOrderService appOrderService;
	@Autowired
	private WebMemberService webMemberService;
	@Autowired
	private WebmTypeService webmTypeService;
	@Autowired
	private MemberOrderService memOrderService;
	@Autowired
    private MemberOrderDetailService memOrderDetailService;
	@Autowired
	private WebMemberDetailService memDetailService;
	@Autowired
	private CateService cateService;
	@Autowired
	private PerformanceService performanceService;
	@Autowired
	private PerformancedetailService performancedetailService;

	
	 @RequestMapping(value="/alipay",method={RequestMethod.POST,RequestMethod.GET})
	 @ResponseBody
	 public ResResult alipay(TokenData data,MemberOrder temp ,HttpServletRequest request,String itemId) throws AlipayApiException{
	    
	    String orderStr = "";         
	    try {

			// 取得用户信息
	        /****** 1.封装你的交易订单开始 *****/                                        //自己用
			//生成订单
			ResResult result = appOrderService.getOrder(temp,data,itemId);
			MemberOrderVo order =(MemberOrderVo) result.getDatas();
			//如果订单生成失败，那么返回失败
			if(result.getCode()!=200){
				return ResResult.error(400, "获取订单信息失败");
			}
			
			
	        
	        Map<String,String> orderMap = new LinkedHashMap<String,String>();            //订单实体
	        
	        /****** 2.商品参数封装开始 *****/                                            //手机端用                
	        //获取订单商品参数
	        MemberOrderDetail memOrderDetail = memOrderDetailService.getMemOrderDetailByOrderNum(order.getOrderNum());
	        if(null==memOrderDetail){
				return ResResult.error(400, "获取商品信息失败");
			}
	        //获取商品详情
	        WebmType webmType = webmTypeService.get(memOrderDetail.getItemId());
	        if(null==webmType){
	        	return ResResult.error(400, "无效商品");
	        }
	        // 商户订单号，商户网站订单系统中唯一订单号，必填
	        orderMap.put("out_trade_no",memOrderDetail.getOrderNum());
	        // 超时时间 可空
	        orderMap.put("timeout_express","30m");
	        // 销售产品码 必填
	        orderMap.put("product_code","QUICK_WAP_PAY");
	        // 订单名称，必填
	        orderMap.put("subject",webmType.getName());
	        // 付款金额，必填
	        orderMap.put("total_amount",memOrderDetail.getTotalPrice()+"");
	        if(DBConst.PAY.equals(order.getOrderType())){
	        	// 商品描述，可空
	        	orderMap.put("body","您支付购买"+webmType.getName() +",共花费"+webmType.getPrice()+"元");
	        }else if(DBConst.DEPOSIT.equals(order.getOrderType())){
	        	orderMap.put("body","您支付定金购买"+webmType.getName() +",共花费"+webmType.getDepositPrice()+"元");
	        }else{
	        	return ResResult.error(4001, "支付异常，请重新提交订单。");
	        }
	        
	        /****** 2.商品参数封装结束 *****/    
	        
	        //实例化客户端  
	        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
	        
	        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay 
	        AlipayTradeAppPayRequestSon ali_request =  new AlipayTradeAppPayRequestSon();  
	        
//	        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。  
//	        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();  
//	        model.setPassbackParams(URLEncoder.encode((String)orderMap.get("body").toString(),"UTF-8"));  //描述信息  添加附加数据  
//	        model.setBody(orderMap.get("body"));                        //商品信息
//	        model.setSubject(orderMap.get("subject"));                  //商品名称
//	        model.setOutTradeNo(orderMap.get("out_trade_no"));          //商户订单号(自动生成)
//	        model.setTimeoutExpress(orderMap.get("timeout_express"));     //交易超时时间
//	        model.setTotalAmount(orderMap.get("total_amount"));         //支付金额
//	        model.setProductCode(orderMap.get("product_code"));         //销售产品码
//	        model.setSellerId(AlipayConfig.APPID);                        //商家id
//	        //ali_request.setBizModel(model);
	        
	        Map<String, String> m = new HashMap<String, String>();
	         m.put("body", orderMap.get("body"));
	         String subject ="猎鹰航空俱乐部VIP";
	         m.put("subject", subject);
	         m.put("out_trade_no", orderMap.get("out_trade_no"));
	         m.put("timeout_express", "30m");         
	         m.put("total_amount", orderMap.get("total_amount"));
	         m.put("product_code", "QUICK_MSECURITY_PAY");
	         
	         
	        ali_request.setNotifyUrl(AlipayConfig.notify_url);          //回调地址  
	        SimpleDateFormat formate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        ali_request.setTimestamp(formate.format(new Date()));
	        ali_request.setVersion("1.0");
	        ali_request.setBizContent(net.sf.json.JSONObject.fromObject(m).toString());
	        AlipayTradeAppPayResponse response = client.sdkExecute(ali_request);  
	        orderStr = response.getBody();  
	        System.err.println(orderStr);                                //就是orderString 可以直接给客户端请求，无需再做处理。  
	        return ResResult.build(200, "订单支付成功", orderStr);
	    } catch (Exception e) {
	    	e.printStackTrace();
	         return ResResult.error(400, "订单支付失败");
	    }
	}
	 
	 /**
	  * 支付宝支付成功后.回调该接口
	  * @param request
	  * @return
	  * @throws IOException
	 * @throws ParseException 
	  */  
	 @RequestMapping(value="notify",method={RequestMethod.POST,RequestMethod.GET})
	 @ResponseBody
	 public String notify(HttpServletRequest request,HttpServletResponse response) throws IOException, ParseException {  
	     Map<String, String> params = new HashMap<String, String>();
	     //1.从支付宝回调的request域中取值
	     Map<String, String[]> requestParams = request.getParameterMap();  
	     
	     for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {  
	         String name = iter.next();  
	         String[] values = requestParams.get(name);  
	         String valueStr = "";  
	         for (int i = 0; i < values.length; i++) {  
	             valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";  
	         }  
	         // 乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化  
	         // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");  
	         params.put(name, valueStr);  
	     }  
	      //2.封装必须参数
	     String out_trade_no = request.getParameter("out_trade_no");            // 商户订单号  
	     //String orderType = request.getParameter("body");                    // 订单内容          
	     String tradeStatus = request.getParameter("trade_status");            //交易状态    
	     
	     //3.签名验证(对支付宝返回的数据验证，确定是支付宝返回的)
	     boolean signVerified = false;  
	     try {  
	         //3.1调用SDK验证签名
	         signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE);     } catch (AlipayApiException e) {  
	         e.printStackTrace();  
	     }  
	     //4.对验签进行处理
	     if (signVerified) {    //验签通过   
	         if(tradeStatus.equals("TRADE_SUCCESS")) {    //只处理支付成功的订单: 修改交易表状态,支付成功
	             MemberOrder order = memOrderService.getMemberOrderByOrderNum(out_trade_no);
	             order.setStatus("1");           //支付完成
	             String returnResult = memOrderService.edit(order);    //更新交易表中状态
	             //根据用户购买订单更新用户的会员状态
	             //判断用户是否是会员
	             WebMember webMember = webMemberService.getMemberByAccount(order.getAccount());
	             //获取用户购买的会员信息
	             MemberOrderDetail orderDetail = memOrderDetailService.getMemOrderDetailByOrderNum(out_trade_no);
	             WebmType webmType = webmTypeService.get(orderDetail.getItemId());
	             WebMemberDetail memberDetail = memDetailService.getMemberDetailByAccount(webMember.getAccount());
	         	
	             //普通用户
	             if(DBConst.MEMBER_GENERAL.equals(webMember.getCategory())){
	            	 //更新用户会员详细信息
	            	 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
	            	 memDetailService.edit(memberDetail);
	            	 System.out.println("=================putong--gou mai hou xiu gai yong hu xiang qing=================================================================");
	            	 //查询上级和上级的上级信息，
	             	//增加上级用户业绩流水等信息
	            	 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
	            	 System.out.println("=================putong--zeng jia ye ji liu shui fen xiao=================================================================");
	            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
	            	 //直接更新用户会员状态
	            	 webMemberService.edit(newWebMember);
	            	 System.out.println("=================putong--geng xin yong hu hui yuan zhuang tai=================================================================");
	            	 
	            	 
	             }else{
	            	 //会员用户
	            	 //判断会员用户的会员级别
	            	 WebmType memType = webmTypeService.get(webMember.getCategory());
	            	 //查询购买级别
	            	 //购买级别比用户级别高
	            	 if(webmType.getLevel()>memType.getLevel()){
	            		 //更新用户会员状态
	            		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
	            		 
		            	 memDetailService.edit(memberDetail);
		            	 System.out.println("=================huiyuan-ji bie gao--xiu gai yong hu xiang qing =================================================================");
		            	 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
		            	 System.out.println("=================huiyuan-ji bie gao--zeng jia yeji =================================================================");
		            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
		            	 //直接更新用户会员状态
		            	 webMemberService.edit(newWebMember);
		            	 System.out.println("=================huiyuan-ji bie gao--geng xin huiyuan =================================================================");
	            	 }else if(webmType.getLevel().equals(memType.getLevel())){
	            		 //级别相同时，判断用户当前会员级别的时间是否为-1（永久）不更新数据，保存订单记录
	            		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
		            	 //更新用户会员详细信息
	            		 memDetailService.edit(memberDetail);
	            		 System.out.println("=================huiyuan-ji bie xiang tong --xiu gai xiang qing =================================================================");

            			 //增加上级用户业绩流水等信息
	            		 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
	            		 System.out.println("=================huiyuan-ji bie xiang tong--ye ji liu shui =================================================================");

		            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
		            	 //直接更新用户会员状态
		            	 webMemberService.edit(newWebMember);
		            	 System.out.println("=================huiyuan-ji bie xiang tong--geng xin huiyuan=================================================================");

	            	 }else{
	            		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
	            		 //增加上级用户业绩流水等信息
	            		 this.addMemberWithdraw(memberDetail, webmType);
	            		 System.out.println("=================huiyuan-ji bie di--zeng jia liu shui fen xiao=================================================================");
	            	 }
	             }
	             
	             if(returnResult.equals(ResponseCode.SUCCESS.getCode())){
	                  return "success";
	             }else{
	                  return "fail";
	             }
	         }else{
	             return "fail";
	         }
	     } else {  //验签不通过   
	         System.err.println("验签失败");
	         return "fail";
	     }
	 }
	 
	 /**
    * 支付宝支付成功后.通知页面
    *@author Zhao
    *@date 2017年11月2日
    *@param request
    *@return
    *@throws UnsupportedEncodingException
     */
    @RequestMapping(value="/returnUrl",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResResult returnUrl(TokenData data,@RequestParam(value = "orderNum") String orderNum,HttpServletRequest request,Model model) throws UnsupportedEncodingException {  
        Map<String,Object>  returnMap = new HashMap<>(); 
        try {
        	// 判断token是不是存在
			if (StringHelper.isEmpty(data.getToken())) {
				return ResResult.error(1, "token不能为空");
			}
			// 先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if (!flag) {
				return ResResult.error(2, "token失效");
			}
			//从token中取account信息   
    		data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
    		//取得用户信息
    		WebMember member = webMemberService.getMemberByAccount(data.getAccount());
            MemberOrder memberOrder = memOrderService.getMemberOrderByOrderNum(orderNum);
             // 返回值Map  
            if(null!=memberOrder&& "1".equals(memberOrder.getStatus())){
            	returnMap.put("member", member);
            	returnMap.put("memberOrder", memberOrder);
            	return ResResult.build(200, "查询成功", returnMap);
            }else{
            	return ResResult.error(200, "查询失败");
            }
        } catch (Exception e) {
        	 return ResResult.error(200, "查询失败");
        }
    }
    
	private Performancedetail addMemberWithdraw(WebMemberDetail memberDetail,WebmType webmType){
		//查询分销费率
     	List<Cate> list = cateService.list(null);
     	Cate cate = list.get(0);
     	//一级分销费率
     	BigDecimal oneLevelCate = cate.getOneLevelCate();
     	//二级分销费率
     	BigDecimal twoLevelCate = cate.getTwoLevelCate();
     	//三级分销费率 
     	BigDecimal thirdLevelCate = cate.getThirdLevelCate();
     	Performance performance = new Performance();
     	Performancedetail performancedetail = new Performancedetail();
		if(null!=memberDetail.getPid()&&!"".equals(memberDetail.getPid())){
     		//判断上级用户是否存在
     		WebMember member2 = webMemberService.getMemberByAccount(memberDetail.getPid());
     		if(null!=member2){
     			//上级用户存在时，查询该用户是否还有上级
     			//增加最上级用户的业绩，余额，流水等信息
 				performance = PayCommon.getNewPerformance(member2, webmType, oneLevelCate);
 				performanceService.add(performance);
 				System.out.println("===============add shang ji ye ji  1111===================================================================");
 				//增加当前用户上级的收益流水
 				performancedetail = PayCommon.getNewPerformancedetail(member2, webmType, oneLevelCate);
 				performancedetailService.add(performancedetail);
 				System.out.println("===============add shang ji ye ji liu liu shui 1111===================================================================");
     			WebMemberDetail memberDetail2 = memDetailService.getMemberDetailByAccount(member2.getAccount());
     			member2.setBalance(member2.getBalance().add(new BigDecimal(performancedetail.getIncome().toString())));
 				webMemberService.edit(member2);
 				System.out.println("===============add shang ji yu e 111===================================================================");
     			WebMember member3 = webMemberService.getMemberByAccount(memberDetail2.getPid());
     			performancedetail.setRevenueSources(memberDetail.getAccount());
     			if(null!=member3){
     				//上级用户存在时，查询该用户是否还有上级
     				//增加最上级用户的业绩，余额，流水等信息
     				performance = PayCommon.getNewPerformance(member3, webmType, twoLevelCate);
     				performanceService.add(performance);
     				System.out.println("===============add shang ji ye ji  2222===================================================================");
     				//增加当前用户的收益流水
     				performancedetail = PayCommon.getNewPerformancedetail(member3, webmType, twoLevelCate);
     				performancedetailService.add(performancedetail);
     				System.out.println("===============add shang ji ye ji liu shui 2222===================================================================");
     				member3.setBalance(member3.getBalance().add(new BigDecimal(performancedetail.getIncome().toString())));
     				webMemberService.edit(member3);
     				System.out.println("===============add shang ji yu e 2222===================================================================");
         			WebMemberDetail memberDetail3 = memDetailService.getMemberDetailByAccount(member3.getAccount());
         			WebMember member4 = webMemberService.getMemberByAccount(memberDetail3.getPid());
         			//判断最上级是否存在
         			if(null!=member4){
         				//增加最上级用户的业绩，余额，流水等信息
         				performance = PayCommon.getNewPerformance(member4, webmType, thirdLevelCate);
         				performanceService.add(performance);
         				System.out.println("===============add shang ji ye ji 3333===================================================================");
         				//增加当前用户的收益流水
         				performancedetail = PayCommon.getNewPerformancedetail(member4, webmType, thirdLevelCate);
         				performancedetailService.add(performancedetail);
         				System.out.println("===============add shang ji ye ji liu shui 3333===================================================================");
         				member4.setBalance(member4.getBalance().add(new BigDecimal(performancedetail.getIncome().toString())));
         				webMemberService.edit(member4);
         				System.out.println("===============add shang ji yu e 3333===================================================================");
         			}
     			}
     		}
     	}
		return performancedetail;
	}
	
	
	/**
	 * 异步通知验证签名 支付宝
	 * 
	 * @return
	 */
	@RequestMapping(value = "successZf")
	@ResponseBody
	public String success(HttpServletRequest request) throws Exception {
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&come to zhifubao！");
		Map<String, String> paramMap = getParamMap(request);
		boolean signVerified = AlipaySignature.rsaCheckV1(paramMap, AlipayConfig.ALIPAY_PUBLIC_KEY, "UTF-8", "RSA2");
		if (signVerified && ("TRADE_SUCCESS".equals(paramMap.get("trade_status"))
				|| "TRADE_FINISHED".equals(paramMap.get("trade_status")))) {
			// 支付宝交易号
			String trade_no = request.getParameter("trade_no");
			String out_trade_no = request.getParameter("out_trade_no");// 商户订单号
			 
			// 按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
		    //只处理支付成功的订单: 修改交易表状态,支付成功
            MemberOrder order = memOrderService.getMemberOrderByOrderNum(out_trade_no);
            order.setStatus("1");           //支付完成
            String returnResult = memOrderService.edit(order);    //更新交易表中状态
            //根据用户购买订单更新用户的会员状态
            //判断用户是否是会员
            WebMember webMember = webMemberService.getMemberByAccount(order.getAccount());
            //获取用户购买的会员信息
            MemberOrderDetail orderDetail = memOrderDetailService.getMemOrderDetailByOrderNum(out_trade_no);
            WebmType webmType = webmTypeService.get(orderDetail.getItemId());
            WebMemberDetail memberDetail = memDetailService.getMemberDetailByAccount(webMember.getAccount());
        	
            //普通用户
            if(DBConst.MEMBER_GENERAL.equals(webMember.getCategory())){
           	 
             	webMemberService.edit(webMember);
           	 //更新用户会员详细信息
           	 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
           	 memDetailService.edit(memberDetail);
           	 //查询上级和上级的上级信息，
            	//增加上级用户业绩流水等信息
           	 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
           	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
           	 //直接更新用户会员状态
           	 webMemberService.edit(newWebMember);
           	 
            }else{
           	 //会员用户
           	 //判断会员用户的会员级别
           	 WebmType memType = webmTypeService.get(webMember.getCategory());
           	 //查询购买级别
           	 //购买级别比用户级别高
           	 if(webmType.getLevel()>memType.getLevel()){
           		 //更新用户会员状态
           		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
           		 memberDetail.setCategory(memType.getId());
	            	 memDetailService.edit(memberDetail);
	            	 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
	            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
	            	 //直接更新用户会员状态
	            	 webMemberService.edit(newWebMember);
           	 }else if(webmType.getLevel()==memType.getLevel()){
           		 //级别相同时，判断用户当前会员级别的时间是否为-1（永久）不更新数据，保存订单记录
           		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
	            	 //更新用户会员详细信息
           		 memDetailService.edit(memberDetail);
       			 //增加上级用户业绩流水等信息
           		 this.addMemberWithdraw(memberDetail, webmType);
           		 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
	            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
	            	 //直接更新用户会员状态
	            	 webMemberService.edit(newWebMember);
           	 }else{
           		 memberDetail = PayCommon.getNewMemberDetail(webMember, webmType,memberDetail);
           		 //增加上级用户业绩流水等信息
           		 this.addMemberWithdraw(memberDetail, webmType);
           		 Performancedetail memberWithdraw = this.addMemberWithdraw(memberDetail, webmType);
	            	 WebMember newWebMember = PayCommon.getNewWebMember(webmType, webMember, memberWithdraw);
	            	 //直接更新用户会员状态
	            	 webMemberService.edit(newWebMember);
           	 }
            }
            
            if(returnResult.equals(ResponseCode.SUCCESS.getCode())){
                 return "success";
            }else{
                 return "fail";
            }
        
	}
        return "success";
	}
	
	public Map<String, String> getParamMap(HttpServletRequest request) {
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String[]> requestParams = request.getParameterMap();
		if (null != requestParams) {
			for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
				String name = (String) iter.next();
				String[] values = (String[]) requestParams.get(name);
				String valueStr = "";
				for (int i = 0; i < values.length; i++) {
					valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
				}
				// 乱码解决，这段代码再出现乱码时使用。如果masign和sign不相等也可以使用这段代码
				params.put(name, valueStr);
		      System.out.println("********name:->"+name+"**********valueStr->"+valueStr);

			}
		}
		return params;
	}
	
	
	public static void main(String[] args) {
		 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); 
		 model.setBody("*****");
		 System.out.println(net.sf.json.JSONObject.fromObject(model).toString());
	}
}
